package com.castudio.Util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckUtils {
    /**
     * 手机号校验
     * @param phone
     * @return
     */
    public static boolean isMobile(String phone) {
        String regex = "^(((\\+86)|(86)|(\\(86\\)))(-?|[ ]?))?(1)\\d{10}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    /**
     * 电话（座机）校验
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone){
        String regex = "^((0\\d{2,3}|\\(0\\d{2,3}\\))-?)?[1-9]\\d{4,7}(-\\d{1,8})?";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        return  matcher.matches();
    }

    /**
     * 邮箱地址校验
     * @param mail
     * @return
     */
    public static boolean isMail(String mail){
        if (isEmptyStr(mail)){
            return false;
        }
        String regex = "\\w+@\\w+\\.\\w+";
        return mail.matches(regex);
    }

    /**
     * 判断字符串是否为数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*\\.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 空字符校验
     * @param str
     * @return
     */
    public static boolean isEmptyStr(String str){
        return (str == null || str.trim().isEmpty());
    }

    /**
     * 整数校验
      * @param number
     * @return boolean
     */
    public static boolean isWholeNumber(String number){
        if (isEmptyStr(number)){
            return false;
        }
        Pattern pattern = Pattern.compile("[+-]?[0-9]+?");
        if (pattern.matcher(number).matches()){
            return true;
        }
        return false;
    }

    /**
     * 正整数校验
     * @param number
     * @return
     */
    public static boolean isPositiveInt(String number){
        if (isEmptyStr(number)){
            return false;
        }
        Pattern pattern = Pattern.compile("[+]?[0-9]+?");
        if (pattern.matcher(number).matches()){
            return true;
        }
        return false;
    }

}
